######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# tableFn13-safe.R
#  :: runs first stage results for to assess whether rain influences elections in safe districts

# Notes: The main results that are interpreted are found in main_fstage1_fe_sumcl and main_fstage2_fe_sumcl

rm(list=ls())
setwd('~/Dropbox/rainReplication')   

# loop over data and fixed effects specifications 
for(fes.type in 2:3){ 
non.missings = 4

source('prelimMain.R')

#detach(covs) 
# ANALYSIS
       
main_fstage1_fe=lm(dose ~ 
	#as.factor(fe_id_num)+
	d_inc+
	extremist + dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + turnout_prv +
	dose_prv + vote_prv + rain_day+rain_day_prev, subset=extr,data=covs)       
  
main_fstage2_fe=lm(dose ~ 
	#as.factor(fe_id_num)+
	d_inc+
	extremist + dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + turnout_prv +
	dose_prv + vote_prv + rain_weekend+rain_weekend_prev, subset=extr,data=covs)	    
        
main_fstage1_fe_sum=summary(main_fstage1_fe)
main_fstage2_fe_sum=summary(main_fstage2_fe)

main_fstage1_fe_sumcl=coeftest.cluster(covs[extr,],main_fstage1_fe,cluster1='as.factor(fe_id_num)')
main_fstage2_fe_sumcl=coeftest.cluster(covs[extr,],main_fstage2_fe,cluster1='as.factor(fe_id_num)')

save(
main_fstage1_fe,
main_fstage2_fe,
main_fstage1_fe_sum,
main_fstage2_fe_sum,
main_fstage1_fe_sumcl,
main_fstage2_fe_sumcl,
file=paste('stratifications/tableFn13_safe-',non.missings,'_',fes.type,'.Rdata',sep='')) 

rm(
main_fstage1_fe,
main_fstage2_fe,
main_fstage1_fe_sum,
main_fstage2_fe_sum,
main_fstage1_fe_sumcl,
main_fstage2_fe_sumcl
)

}      

# END